import 'dart:io';

class Ticker {
  const Ticker();
  Stream<String> tick() async* {
    while (true) {
      yield await trackActiveWindow();
    }
  }
  Stream<void> tack() async* {
    while (true) {
      final d = await Future.delayed(const Duration(seconds: 1)); 
      yield d;      
    }
  }

  Stream<List<String>> tickBack() async* {
    while (true) {
      final a =  await trackBackgroudWindows();
      yield  a;
    }
  }

  Future<List<String>> trackBackgroudWindows() async {
    await Future.delayed(const Duration(seconds: 1));
    var result = await Process.run('wmctrl', ['-lip']);
    var output = result.stdout.toString();
    
    var lines = output.split('\n');
    return lines.map((line) {
      var reg = RegExp(r"(\S+)");
      var fields = reg.allMatches(line).map((e) => e[0]).toList();
      var variable = '';
      for (var i = 4; i < fields.length; i++) {
        if (variable != '') {
          variable += ' ';
        }
        variable += '${fields[i]} ';
      }
      final re = RegExp(r"(?:\B +)?(\w[\w| ]+)$");
      String appName = (re.hasMatch(variable)
          ? re.allMatches(variable).first[0]!
          : variable);
      appName = appName.trimLeft().trimRight();
      return appName;
    }).toList();
  }

  static List<String> statTrackBackgroudWindows() {
    var result = Process.runSync('wmctrl', ['-lip']);
    var output = result.stdout.toString();
    
    var lines = output.split('\n');
    return lines.map((line) {
      var reg = RegExp(r"(\S+)");
      var fields = reg.allMatches(line).map((e) => e[0]).toList();
      var variable = '';
      for (var i = 4; i < fields.length; i++) {
        if (variable != '') {
          variable += ' ';
        }
        variable += '${fields[i]} ';
      }
      final re = RegExp(r"(?:\B +)?(\w[\w| ]+)$");
      String appName = (re.hasMatch(variable)
          ? re.allMatches(variable).first[0]!
          : variable);
      appName = appName.trimLeft().trimRight();
      return appName;
    }).toList();
  }

  Future<String> trackActiveWindow() async {
    await Future.delayed(const Duration(seconds: 1));
    int wid =
        int.parse((await Process.run('xdotool', ['getactivewindow'])).stdout);
    int pid = int.parse(
        (await Process.run('xdotool', ['getwindowpid', wid.toString()]))
            .stdout);
    var result = await Process.run('wmctrl', ['-lip']);
    var output = result.stdout.toString();
    var variable = '';
    var lines = output.split('\n');
    for (var line in lines) {
      if (line.contains(pid.toString())) {
        var reg = RegExp(r"(\S+)");
        var fields = reg.allMatches(line).map((e) => e[0]).toList();

        for (var i = 4; i < fields.length; i++) {
          if (variable != '') {
            variable += ' ';
          }
          variable += '${fields[i]} ';
        }
        break;
      }
    }
    final re = RegExp(r"(?:\B +)?(\w[\w| ]+)$");
    String appName =
        (re.hasMatch(variable) ? re.allMatches(variable).first[0]! : variable);
    appName = appName.trimLeft().trimRight();
    return appName;
  }
}
